import time

from DrissionPage import Chromium
import ddddocr

ocr = ddddocr.DdddOcr()

DEBUG = True

tab = Chromium().latest_tab

while True:

	tab.get("https://www.gushiwen.cn/user/login.aspx")

	email_ele = tab.ele('x=//input[@id="email"]')
	email_ele.input("18503800568")

	pwd_ele = tab.ele('x=//input[@id="pwd"]')
	pwd_ele.input("qikuedu123")

	code_ele = tab.ele('x=//input[@id="code"]')

	imgCode_ele = tab.ele('x=//img[@id="imgCode"]')
	code_bytes = imgCode_ele.get_screenshot(as_bytes='jpg')
	code = ocr.classification(code_bytes)
	if DEBUG:
		with open(f"./codes/gsw_{code}.jpg", "wb") as f:
			f.write(code_bytes)

	code_ele.input(code)

	denglu_ele = tab.ele('x=//input[@id="denglu"]')
	denglu_ele.click()

	try:
		if "收藏" in tab.ele('x=//title').text:
			print(f"登录成功.........")
			break
	except Exception as e:
		tab.handle_alert(True)

